package sort.record;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author masaru-gotou
 *
 */
@SuppressWarnings("serial")
public class Records implements Iterable<Record>,Serializable {
	/**
	 * 
	 */
	private List<Record> list = new ArrayList<Record>();
	
	public Iterator<Record> iterator() {
		return list.iterator();
	}

	/**
	 * @param e
	 * @return
	 * @see java.util.List#add(java.lang.Object)
	 */
	public boolean add(Record e) {
		return list.add(e);
	}

	/**
	 * @param index
	 * @return
	 * @see java.util.List#get(int)
	 */
	public Record get(int index) {
		return list.get(index);
	}

	/**
	 * @param index
	 * @param element
	 * @return
	 * @see java.util.List#set(int, java.lang.Object)
	 */
	public Record set(int index, Record element) {
		list.get(index).setKey(element.getKey());
		list.get(index).setValue(element.getValue());
//		notifyObservers();
		return list.get(index);
	}

	/**
	 * @return
	 * @see java.util.List#size()
	 */
	public int size() {
		return list.size();
	}

	public Records copy() {
		Records copy = new Records();
		for ( Record rec : this.list ) {
			copy.add(new Record(rec));
		}
		return copy;
	}

	/**
	 * @param c
	 * @return
	 * @see java.util.List#containsAll(java.util.Collection)
	 */
	public boolean containsAll(Records recs) {
		return list.containsAll(recs.list);
	}
	
	/**
	 * 
	 * @param key
	 * @return
	 */
	public Records getRecordsByKey(int key) {
		Records subset = new Records();
		for ( Record rec : this.list ) {
			if ( rec.getKey() == key ) {
				subset.add(rec);
			}
		}
		return subset;
	}
	public String toString() {
		StringBuffer buf = new StringBuffer();
		final String BR = "\n";
		
		buf.append("recs="+list.size()).append(BR);
		for ( int i=0; i<list.size(); i++ ) {
			buf.append("["+i+"]"+list.get(i).toString()).append(BR);
		}
		return buf.toString();
	}
}
